# 结合使用try-except-finally
# 无论发生什么情况.finally block中的语句都会被执行,哪怕前面的try和except block中使用了return语句
# finally机制多用于释放资源
import sys


def read_file(path):
    try:
        f = open(path, 'r')
        text = f.read()
        print(text)
    except OSError as err:
        print(f'OSError: {err}')
    except:
        print('unexpected exception: ', sys.exc_info()[0])
    # 在finally block中释放资源
    finally:
        f.close()


if __name__ == '__main__':
    read_file('in.txt')
